package com.yanqu.road.server.manager.tomb.dao;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.server.manager.tomb.entity.DbTombLog;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TombLogDaoImpl extends TempDataObjectDao<DbTombLog> {


    public static TombLogDaoImpl newInstance() {
        TombLogDaoImpl dao = new TombLogDaoImpl();
        return dao;
    }

    public static String updateSql = "update `t_c_activity_tomb_log` set fromUserId=?,toUserId=?,logEnum=?,rewards=?,energyChange=?,energy=?,eventId=?,floor=?,x=?,y=?,value=?,value1=?,status=?,description=?,updateTime=?,groupId=? where id=?";

    @Override
    protected boolean updateData(DbTombLog data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getFromUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getToUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getLogEnum()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getRewards()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getEnergyChange()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getEnergy()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getX()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getY()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getValue1()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getDescription()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));

        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getId()));

        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSql = "insert into `t_c_activity_tomb_log`(id,activityId,uid,fromUserId,toUserId,logEnum,rewards,energyChange,energy,eventId,floor,x,y,value,value1,status,description,createTime,updateTime,groupId) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DbTombLog data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getUid()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getFromUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getToUserId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getLogEnum()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getRewards()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getEnergyChange()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getEnergy()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getX()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getY()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getValue1()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getStatus()));
        dbParameterMap.put(i++, new DbParameter(Types.VARCHAR, data.getDescription()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected DbTombLog parseData(ResultSet rs) throws SQLException {
        DbTombLog data = DbTombLog.newInstance();
        data.setId(rs.getLong("id"));
        data.setActivityId(rs.getInt("activityId"));
        data.setUid(rs.getString("uid"));
        data.setFromUserId(rs.getLong("fromUserId"));
        data.setToUserId(rs.getLong("toUserId"));
        data.setLogEnum(rs.getInt("logEnum"));
        data.setRewards(rs.getString("rewards"));
        data.setEnergyChange(rs.getLong("energyChange"));
        data.setEnergy(rs.getLong("energy"));
        data.setEventId(rs.getInt("eventId"));
        data.setFloor(rs.getInt("floor"));
        data.setX(rs.getInt("x"));
        data.setY(rs.getInt("y"));
        data.setValue(rs.getLong("value"));
        data.setValue1(rs.getLong("value1"));
        data.setStatus(rs.getInt("status"));
        data.setDescription(rs.getString("description"));
        data.setGroupId(rs.getInt("groupId"));
        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));

        return data;
    }

    public DbTombLog getData(long id) {
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_c_activity_tomb_log where id = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, id);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Map<Long, DbTombLog> getDataMap(int activityId, String uid, int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, DbTombLog> dataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_c_activity_tomb_log where activityId = ? and uid = ? ORDER BY createTime desc limit ?";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, activityId);
            call.setString(2, uid);
            call.setInt(3, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                DbTombLog data = parse(rs);
                dataMap.put(data.getId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean saveData(DbTombLog data) {
        if (data.isUpdateOption()) {
            if (data.isNeedInsert()) {
                if (addData(data)) {
                    data.setNeedInsert(false);
                }
            } else {
                data.setUpdateTime(System.currentTimeMillis());
                update(data);
            }
        }
        return true;
    }

}
